// 通用工具函数
import * as cornerstone from '@cornerstonejs/core';

/**
 * 初始化演示
 */
export function initDemo() {
  console.log('Demo initialized');
}

/**
 * 创建图像ID并缓存元数据
 * @param {Object} options - 选项对象
 * @param {Array} options.imageIds - 图像ID数组
 * @param {string} options.StudyInstanceUID - 检查实例UID
 * @param {string} options.SeriesInstanceUID - 系列实例UID
 * @param {string} options.SOPInstanceUID - SOP实例UID
 * @returns {Promise} 处理Promise
 */
export async function createImageIdsAndCacheMetaData(options) {
  const { imageIds, StudyInstanceUID, SeriesInstanceUID, SOPInstanceUID } = options;
  
  // 为每个图像ID缓存元数据
  imageIds.forEach((imageId, index) => {
    // 缓存图像平面模块元数据
    cornerstone.metaData.addProvider((type, imageId) => {
      if (type === 'imagePlaneModule') {
        return {
          frameOfReferenceUID: StudyInstanceUID,
          rows: 512,
          columns: 512,
          imageOrientationPatient: [1, 0, 0, 0, 1, 0],
          imagePositionPatient: [0, 0, index * 5],
          pixelSpacing: [1, 1],
          rowCosines: [1, 0, 0],
          columnCosines: [0, 1, 0],
          rowPixelSpacing: 1,
          columnPixelSpacing: 1
        };
      }
      
      if (type === 'imageSliceModule') {
        return {
          sliceThickness: 5,
          sliceLocation: index * 5
        };
      }
      
      if (type === 'generalStudyModule') {
        return {
          studyInstanceUID: StudyInstanceUID,
          studyDate: '20240101',
          studyTime: '120000',
          patientName: 'Test Patient',
          patientId: 'TEST001',
          seriesNumber: 1,
          seriesDescription: 'Test Series'
        };
      }
      
      if (type === 'sopCommonModule') {
        return {
          sopInstanceUID: SOPInstanceUID,
          instanceNumber: index + 1
        };
      }
      
      return undefined;
    });
  });
  
  return Promise.resolve();
}


/**
 * 设置标题和描述
 * @param {string} title - 标题
 * @param {string} description - 描述
 */
export function setTitleAndDescription(title, description) {
  document.title = title;
  const descElement = document.querySelector('meta[name="description"]');
  if (descElement) {
    descElement.setAttribute('content', description);
  }
}

/**
 * 格式化文件大小
 * @param {number} bytes - 字节数
 * @returns {string} 格式化后的文件大小
 */
export function formatFileSize(bytes) {
  if (bytes === 0) return '0 Bytes';
  
  const k = 1024;
  const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}

/**
 * 防抖函数
 * @param {Function} func - 要防抖的函数
 * @param {number} wait - 等待时间
 * @returns {Function} 防抖后的函数
 */
export function debounce(func, wait) {
  let timeout;
  return function executedFunction(...args) {
    const later = () => {
      clearTimeout(timeout);
      func(...args);
    };
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
  };
}

/**
 * 节流函数
 * @param {Function} func - 要节流的函数
 * @param {number} limit - 限制时间
 * @returns {Function} 节流后的函数
 */
export function throttle(func, limit) {
  let inThrottle;
  return function(...args) {
    if (!inThrottle) {
      func.apply(this, args);
      inThrottle = true;
      setTimeout(() => inThrottle = false, limit);
    }
  };
}